// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Client Side logic
// -----------------------------------------------
if (isDedicated) exitWith {};

private ["_end","_kindofgame","_marker","_end2","_end3","_trgmenuoption"];

// Init global variables
wcgarbage = [] call WC_fnc_clientinitconfig;

// Call intro cam
wcanim = [] spawn WC_fnc_intro;

// Initialize musics
wcjukebox = [] call WC_fnc_enummusic;

// Grab all WC_fnc_publicvariable events
wcgarbage = [] spawn WC_fnc_eventhandler;
wcgarbage = [] spawn WC_fnc_clienthandler;

// Init action menu
wcgarbage = [] call WC_fnc_restoreactionmenu;

sleep 1;

// Notify server that player is ready
wcplayerreadyadd = name player;
["wcplayerreadyadd", "server"] call WC_fnc_publicvariable;

// Intialize light depending weather
if (wcwithlight == 1) then {
	wcgarbage = [] spawn WC_fnc_light;
};

waitUntil {!isNull player};
waitUntil {format ["%1", typeOf player] != "any"}; 
player setPos getMarkerPos "respawn_west";

if (format ["%1", wcselectedzone] == "any") then {wcselectedzone = [0,0,0]};

// By default wc uses R3F revive
if (true) then {
	execVM "extern\R3F_revive\revive_init.sqf";
} else {
	R3F_REV_nb_reanimations = 0;
	player addEventHandler ["Killed", {wcgarbage = [] spawn WC_fnc_onkilled}];
};

// Load player HUD
wcgarbage = [] spawn WC_fnc_lifeslider;

_marker = ["rescue", 0.01, [0,0,0], "ColorRed", "ICON", "FDIAGONAL", "Selector_selectedMission", 0, "", false] call WC_fnc_createmarkerlocal;
wcgarbage = [_marker] spawn WC_fnc_markerhintlocal;

wcrespawnmarker = ["respawn", 0.5, [0,0,0], "ColorRed", "ICON", "FDIAGONAL", "Camp", 0, "", false] call WC_fnc_createmarkerlocal;
wcrespawnmarker setMarkerSizeLocal [0,0];

// Arcade == 1
if (wckindofgame == 1) then {
	wcgarbage = ["Hospital", getMarkerPos "hospital"] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Weapons", getMarkerPos "crate1"] spawn BIS_fnc_3Dcredits;
	if (wcautoloadweapons == 1) then {
		wcgarbage = ["Addons Weapons", getMarkerPos "autoloadcrate"] spawn BIS_fnc_3Dcredits;
	};
	wcgarbage = ["Presets", getPosATL preset] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Repair center", getMarkerPos "repair"] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Recruitment", getMarkerPos "recruit1"] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Hall of fames", getPos teammanage] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Clothes", getMarkerPos "clothes"] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Jail", getMarkerPos "jail"] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Headquarters", getPos anim] spawn BIS_fnc_3Dcredits;
	wcgarbage = ["Ied training", getPos iedtraining] spawn BIS_fnc_3Dcredits;
};

_end = createTrigger ["EmptyDetector", wcmapcenter];
_end setTriggerArea [10, 10, 0, false];
_end setTriggerActivation ["CIV", "PRESENT", true];
_end setTriggerStatements ["(wcteamscore < wcscorelimitmin)", "
	wcanim = [] execVM 'warcontext\camera\WC_fnc_outrolooser.sqf';
", ""];

_end2 = createTrigger ["EmptyDetector", wcmapcenter];
_end2 setTriggerArea [10, 10, 0, false];
_end2 setTriggerActivation ["CIV", "PRESENT", true];
_end2 setTriggerStatements ["(wcteamscore > wcscorelimitmax)", "
	wcanim = [] execVM 'warcontext\camera\WC_fnc_outro.sqf';
", ""];

_end3 = createTrigger ["EmptyDetector", wcmapcenter];
_end3 setTriggerArea [10, 10, 0, false];
_end3 setTriggerActivation ["CIV", "PRESENT", true];
_end3 setTriggerStatements ["(wclevel > (wclevelmax - 1))", "
	wcanim = [] execVM 'warcontext\camera\WC_fnc_outro.sqf';
", ""];

// Menu options when player is in vehicle
_trgmenuoption = createTrigger ["EmptyDetector", getPosATL player];
_trgmenuoption setTriggerArea [0, 0, 0, false];
_trgmenuoption setTriggerActivation ["NONE", "PRESENT", true];
_trgmenuoption setTriggerTimeout [5, 5, 5, false];
_trgmenuoption setTriggerStatements [
"vehicle player != player", 
"wcvehicle = vehicle player; 
wcactionmenuoption = wcvehicle addAction ['<t color=''#ff4500''>Mission Info</t>', 'warcontext\dialogs\WC_fnc_createmenumissioninfo.sqf', [], 5, false]; wcgarbage = [] spawn WC_fnc_checkpilot; enableEnvironment false;", 
"wcvehicle removeAction wcactionmenuoption; if (wcwithenvironment == 1) then {enableEnvironment true};"];

wcgarbage = [] spawn {
	private ["_ranked","_oldlevel","_teampromote","_message"];

	if (wckindofgame == 1) then {
		_ranked = [
			-60,
			-40,
			-20,
			0,
			20,
			40,
			60,
			80
		];
	} else {
		_ranked = [
			-15,
			-10,
			-5,
			0,
			5,
			10,
			15,
			20
		];
	};

	_oldlevel = 5;
	while {true} do {
		wcteamlevel = 8;
		if (wcteamscore >= (_ranked select 1)) then { wcteamlevel = 7 };
		if (wcteamscore >= (_ranked select 2)) then { wcteamlevel = 6 };
		if (wcteamscore >= (_ranked select 3)) then { wcteamlevel = 5 };
		if (wcteamscore >= (_ranked select 4)) then { wcteamlevel = 4 };
		if (wcteamscore >= (_ranked select 5)) then { wcteamlevel = 3 };
		if (wcteamscore >= (_ranked select 6)) then { wcteamlevel = 2 };
		if (wcteamscore >= (_ranked select 7)) then { wcteamlevel = 1 };
		if (_oldlevel != wcteamlevel) then {
			if (_oldlevel > wcteamlevel) then {
				_oldlevel = wcteamlevel;
				playSound "drum";
				_teampromote = localize format ["STR_WC_TEAM%1", wcteamlevel];
				_message = [localize "STR_WC_MESSAGETEAMPROMOTED", format ["to %1 !", _teampromote]];
				wcgarbage = _message spawn EXT_fnc_infotext;
			} else {
				_oldlevel = wcteamlevel;
				playSound "drum";
				_teampromote = localize format ["STR_WC_TEAM%1", wcteamlevel];
				_message = [localize "STR_WC_MESSAGETEAMDEGRADED", format ["to %1 !", _teampromote]];
				wcgarbage = _message spawn EXT_fnc_infotext;
			};
		};
		sleep 5;
	};
};

// Load player team preset
wcgarbage = [] spawn WC_fnc_clientitems;
wcgarbage = [] spawn WC_fnc_creatediary;

wcgarbage = [(getMarkerPos "crate1"), "base"] spawn WC_fnc_createammobox;
if (wcautoloadweapons == 1) then {
	wcgarbage = [(getMarkerPos "autoloadcrate"), "addons"] spawn WC_fnc_createammobox;
};

if (wcwithmarkers == 1) then {
	wcgarbage = [] spawn WC_fnc_playersmarkers;
	wcgarbage = [] spawn WC_fnc_vehiclesmarkers;
};

// Syncrhonize players rank
{
	(_x select 0) setRank (_x select 1);
} foreach wcranksync;

wcgarbage = [] spawn {
	while {true} do {
		if (rating player < 0) then {
			if (driver (vehicle player) == player) then {
				player addRating (rating player * -1);
				wcgarbage = ["You have got a blame"] call BIS_fnc_dynamicText;
				wctk = name player;
				["wctk", "server"] call WC_fnc_publicvariable;
			};
		} else {
			if (rating player < 3000) then {
				player addRating 3000;
			};
		};
		sleep 4;
	};
};

// Personnal rank
wcgarbage = [] spawn WC_fnc_playerranking;

// Base hospital
wcgarbage = [] spawn {
	private ["_message"];
	while {true} do {
		if ((getPosATL player) distance (getMarkerPos "hospital") < 5) then {
			_message = ["You retrieve", format ["your %1 revives", R3F_REV_CFG_nb_reanimations]];
			wcgarbage = _message spawn EXT_fnc_infotext;
			R3F_REV_nb_reanimations = R3F_REV_CFG_nb_reanimations;
			player setDamage 0;
			wcclientlogs = wcclientlogs + [format ["Hospital: you retrieve your %1 revives", R3F_REV_CFG_nb_reanimations]];
			while {((getPosATL player) distance (getMarkerPos "hospital") < 5)} do {
				sleep 1;
			};
		};
		sleep 10;
	};
};

// Teamplay bonus
wcgarbage = [] spawn {
	private ["_message"];
	while {true} do {
		{
			if ((_x distance player < 100) and (_x != player)) then {
				wcbonus = wcbonus + 1;
			};
			sleep 0.1;
		} foreach playableUnits;
		if (wcbonus > 10000) then {
			_message = ["You have win", "10 points Teamplay bonus"];
			wcgarbage = _message spawn EXT_fnc_infotext;
			wcbonus = 0;
			wcplayeraddscore = [player, 10];
			["wcplayeraddscore", "server"] call WC_fnc_publicvariable;
			wcclientlogs = wcclientlogs + ["Teamplay bonus: 10 personnal points"];
		};
		sleep 1;
	};
};

// addAction target graber
wcgarbage = [] spawn WC_fnc_targetaction;

// Transfer point
wcgarbage = [] spawn {
	// If not noteam server
	if (wckindofserver != 3) then {
		while {true} do {
			if (wcteamplayscore > 29) then {
				wcgarbage = ["Share points", format [localize "STR_WC_TRANSFERTPOINT", wcteamplayscore], localize "STR_WC_SHAREPOINTS", 10] spawn WC_fnc_playerhint;
				sleep 10;
			} else {
				if (wcteamplayscore > 19) then {
					wcgarbage = ["Share points", format [localize "STR_WC_TRANSFERTPOINT", wcteamplayscore], localize "STR_WC_SHAREPOINTS", 10] spawn WC_fnc_playerhint;
					sleep 30;
				} else {
					if (wcteamplayscore > 9) then {
						wcgarbage = ["Share points", format [localize "STR_WC_TRANSFERTPOINT", wcteamplayscore], localize "STR_WC_SHAREPOINTS", 10] spawn WC_fnc_playerhint;
						sleep 60;
					} else {
						if (wcteamplayscore > 0) then {
							wcgarbage = ["Share points", format [localize "STR_WC_TRANSFERTPOINT", wcteamplayscore], localize "STR_WC_SHAREPOINTS", 10] spawn WC_fnc_playerhint;
							sleep 120;
						} else {
							sleep 60;
						};
					};
				};
			};
		};
	};
};

// Check if player is admin
wcgarbage = [] spawn {
	while {true} do {
		wcadmin = serverCommandAvailable "#kick";
		if (serverCommandAvailable "#kick") then {
			wcadmin = true;
		} else {
			if ((getPlayerUID player) in wcteammembers) then {
				wcadmin = true;
			} else {
				wcadmin = false;
			};
		};
		sleep 5;
	};
};

//A3 TODO: Redo cursorTarget
// Drag & drop body
wcgarbage = [] spawn {
	private ["_attached","_unit","_units","_count","_animation"];

	_attached = false;
	_animation = ["amovppnemstpsnonwnondnon_healed","amovppnemstpsnonwnondnon_injured"];
	_count = 0;

	while {true} do {
		if !(_attached) then { 
			_units = nearestObjects [player, ["CAManBase"], 2];
			if (count _units == 2) then {
				{
					if ((_x != player) and ((animationState _x) in _animation)) then {
						if (isNil "wcdragg") then {
							wcdragg = player addAction ["<t color='#dddd00'>Drag player</t>", "warcontext\actions\WC_fnc_dodrag.sqf", [], -1, false];
						};
						if (wcdragged) then {
							player playMove "acinpknlmstpsraswrfldnon";
							_x attachTo [player, [0.1, 1.01, 0]];
							_attached = true;
							_unit = _x;
						};
					};
				} foreach _units;
			} else {
				player removeAction wcdragg;
				wcdragg = nil;
			};
		};
		if (_attached) then {
			_count = _count + 1;
		};
		if (_count > 10) then {
			wcdragged = false;
			_attached = false;
			detach _unit;
			_count = 0;
			player playAction "released";
			sleep 2;
		};
		sleep 1;
	};
};

// Autoload warning messages if desync with server
wcgarbage = [] spawn {
	waitUntil {format ["%1", wccfgpatches] != "any"};
	wccfglocalpatches =  wccfgpatches;
	{
		if !(isClass (configFile >> "CfgPatches" >> _x)) then {
			player sideChat format [localize "STR_WC_MESSAGEMISSINGADDONS", _x];
			sleep 1;
		} else {
			wccfglocalpatches = wccfglocalpatches - [_x];
		};
	} foreach wccfglocalpatches;

	if (count wccfglocalpatches > 0) then {
		sleep 30;
		wcgarbage = [localize "STR_WC_MESSAGEWARNING", localize "STR_WC_MESSAGERESTARTGAME", localize "STR_WC_MESSAGENOTSYNC", 60] spawn WC_fnc_playerhint;
	};
};

// Ied detector
wcgarbage = [] spawn WC_fnc_ieddetector;

// Nuclear zone - radiation
if (wcwithnuclear == 1) then {
	wcgarbage = [] spawn {
		while {true} do {
			{
				if ((player distance _x < 500) and !wcplayerinnuclearzone) then {
					wcgarbage = [_x] spawn WC_fnc_createnuclearzone;
				};
			} foreach wcnuclearzone;
			sleep 1;
		};
	};
};

//A3 TODO: Redo cursorTarget
// Everybody is medic
wcgarbage = [] spawn {
	private ["_injured","_men"];
	wcmedicmenu = nil;
	while {true} do {
		if ((wceverybodymedic == 1) and !(typeOf player in wcmedicclass)) then {
			_men = nearestObjects [player, ["CAManBase"], 5];
			_injured = objNull;
			{
				if ((damage _x > 0.2) and !(_x == player)) then {
					_injured = _x;
				};
			} foreach _men;

			if (!(isNull _injured) and (isNil "wcmedicmenu"))  then {
				wcmedicmenu = player addAction ["<t color='#dddd00'>Heal</t>", "warcontext\actions\WC_fnc_doheal.sqf", [_injured], 6, false];
			} else {
				if !(isNil "wcmedicmenu") then {player removeAction wcmedicmenu; wcmedicmenu = nil};
			};
		};
		sleep 5;
	};
};

// If player die, end of game for one life mission
if (wcwithonelife == 1) then {
	_end = createTrigger ["EmptyDetector", [4000,4000,0]];
	_end setTriggerArea [10, 10, 0, false];
	_end setTriggerActivation ["CIV", "PRESENT", true];
	_end setTriggerStatements ["!alive player && local player", "
		tskExample1 = player createSimpleTask ['Task Message'];
		tskExample1 setSimpleTaskDescription ['Task Message', 'You have been killed', 'You have been killed'];
		wctoonelife = name player;
		['wctoonelife', 'server'] call WC_fnc_publicvariable;
	", ""];
	_end setTriggerType "END1";

	wcgarbage = [] spawn {
		while {true} do {
			if ((name player) in wconelife) then {
				removeAllWeapons player;
				removeAllItems player;
				player enableSimulation false;
			};
			sleep 10;
		};
	};
};

sleep 5;

switch (wckindofgame) do {
	case 1: {
		_kindofgame = "ARCADE";
	};

	case 2: {
		_kindofgame = "SIMULATION";	
	};

	case 3: {
		_kindofgame = "PRACTICE";
	};
};

wcclientlogs = wcclientlogs + [localize "STR_WC_MESSAGEMISSIONINITIALIZED"];

// Initialize player score on server
sleep 30;

wcgarbage = [localize "STR_WC_MENUWELCOMEBASE", localize "STR_WC_MENUTAKEWEAPONS", format [localize "STR_WC_MENUKINDOFGAME", _kindofgame], 10] spawn WC_fnc_playerhint;

wcplayeraddscore = [player, -1];
["wcplayeraddscore", "server"] call WC_fnc_publicvariable;